import java.util.LinkedList;

/**
 * Created with IntelliJ IDEA.
 * Description: LinkedList链表测试
 */

class MySingleList {

    /**
     * 节点内部类
     */
    static class ListNode {
        public int val;
        public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }

    public ListNode head;//不初始化了 默认就是null

    public void createList() {
        ListNode listNode1 = new ListNode(12);
        ListNode listNode2 = new ListNode(23);
        ListNode listNode3 = new ListNode(34);
        ListNode listNode4 = new ListNode(45);
        ListNode listNode5 = new ListNode(56);
        listNode1.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;
        this.head = listNode1;
    }

    /**
     * 打印链表里面的数据
     */
    public void display() {
        ListNode cur = this.head;
        while (cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    /**
     * 得到单链表的长度
     * @return 返回长度
     */
    public int size(){
        int size = 0;
        ListNode cur = head;
        while (cur != null) {
            size++;
            cur = cur.next;
        }
        return size;
    }

    //头插法
    public void addFirst(int data){
        ListNode newNode = new ListNode(data);
        newNode.next = head;
        head = newNode;
    }
    //尾插法
    public void addLast(int data){
        ListNode temp = new ListNode(data);
        if(head == null) {
            head = temp;
            return;
        }
        ListNode cur = head;
        while(cur.next != null) {
            cur = cur.next;
        }
        cur.next = temp;
    }

    /**
     * 任意位置插入,第一个数据节点为0号下标
     * @param index 插入的坐标
     * @param data 插入节点的val值
     */
    public boolean addIndex(int index,int data){
        if(index == 0) {
            addFirst(data);
            return true;
        }
        try {
            if(index > size()){
                throw new InsertOutBoundException();
            }
        }catch (InsertOutBoundException e) {
            e.printStackTrace();
        }
        ListNode cur = this.head;
        ListNode newNode = new ListNode(data);
        while(index - 1 != 0) {
            cur = cur.next;
            index--;
        }
        newNode.next = cur.next;
        cur.next = newNode;
        return true;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(this.head == null) {
            return false;
        }
        ListNode cur = this.head;
        while(cur != null) {
            if(cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if(this.head == null) {
            return;
        }
        if(head.val == key) {
            head = head.next;
            return;
        }
        ListNode cur = this.head;
        while(cur.next != null) {
            if(cur.next.val == key) {
                cur.next = cur.next.next;
                return;
            }
            cur = cur.next;
        }
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        while(this.head != null && this.head.val == key) {
            this.head = this.head.next;
        }
        if(this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = this.head.next;
        while(cur != null) {
            if(cur.val == key) {
                prev.next = cur.next;
            } else {
                prev = cur;
            }
            cur = cur.next;
        }
    }


    public void clear() {
        head = null;
    }

    //返回链表倒数第k个节点
    public ListNode FindKthToTail(ListNode head,int k) {
        ListNode first = head;
        ListNode slow = head;
        if(k <= 0) {
            return null;
        }
        while(k - 1 != 0 && first != null) {
            first = first.next;
            k--;
        }
        if(first == null) {
            return null;
        }
        while(first.next != null) {
            first = first.next;
            slow = slow.next;
        }
        return slow;
    }

    //返回链表中间节点
    public ListNode middleNode(ListNode head) {
        ListNode first = head;
        ListNode slow = head;
        if(head == null || head.next == null) {
            return head;
        }
        while(first != null && first.next != null) {
            first = first.next.next;
            slow = slow.next;
        }
        return slow;
    }

    //反转链表
    public ListNode reverseList(ListNode head) {
        if(head == null ||  head.next == null) {
            return head;
        }
        ListNode temp = head.next;
        head.next = null;
        while(temp != null) {
            ListNode headOld = temp.next;
            temp.next = head;
            head = temp;
            temp = headOld;
        }
        return head;
    }

    //判断回文串
    public boolean chkPalindrome(ListNode A) {
        // write code here
        if(A == null) {
            return false;
        }
        //先得到中间节点
        ListNode fast = A;
        ListNode slow = A;
        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //以中间节点为准，将后面的链表翻转
        ListNode cur = slow.next;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //判断是否是回文串
        while(A != slow) {
            if(A.val != slow.val) {
                return false;
            }
            //判断偶数情况
            if(A.next == slow) {
                return true;
            }
            A = A.next;
            slow = slow.next;
        }
        return true;
    }

    //环形链表Ⅱ
    //返回链表开始入环的第一个节点，若链表无环，则返回null
    //写法一，找到入口点或没有环结束 while ，再进行判断
    public ListNode detectCycle1(ListNode head) {
        ListNode first = head;
        ListNode slow = head;
        if(head == null || head.next == null) {
            return null;
        }
        while(first != null && first.next != null) {
            first = first.next.next;
            slow = slow.next;
            if(first == slow) {
                break;
            }
        }
        if(first != slow) {
            return null;
        }
        slow = head;
        while(slow != first) {
            slow = slow.next;
            first = first.next;
        }
        return slow;
    }

    //写法二，直接在 while 循环内找到入口点
    public ListNode detectCycle2(ListNode head) {
        ListNode first = head;
        ListNode slow = head;
        while(first != null && first.next != null) {
            first = first.next.next;
            slow = slow.next;
            if(first == slow) {
                slow = head;
                while(slow != first) {
                    slow = slow.next;
                    first = first.next;
                }
                return slow;
            }
        }
        return null;
    }

    //环形链表Ⅰ
    //判断链表是否有环
    public boolean hasCycle(ListNode head) {
        ListNode first = head;
        ListNode slow = head;
        while(first != null && first.next != null) {
            first = first.next.next;
            slow = slow.next;
            if(slow == first) {
                return true;
            }
        }
        return false;
    }


    // 链表分割
    //方法一
    public ListNode partition1(ListNode pHead, int x) {
        // write code here
        if(pHead == null || pHead.next == null) {
            return pHead;
        }
        ListNode listf1 = null;
        ListNode liste1 = null;
        ListNode listf2 = null;
        ListNode liste2 = null;
        ListNode cur = pHead;
        while(cur != null) {
            if(cur.val < x) {
                //放在前面的链表
                if(listf1 == null) {
                    //前面链表的头节点还是 null 的情况下
                    listf1 = liste1 = cur;
                } else {
                    liste1.next = cur;
                    liste1 = liste1.next;
                }
            } else {
                //放在后面的链表
                if(listf2 == null) {
                    //后面链表的头节点还是 null 的情况下
                    listf2 = liste2 = cur;
                } else {
                    liste2.next = cur;
                    liste2 = liste2.next;
                }
            }
            cur = cur.next;
        }
        if(listf2 != null) {
            liste2.next = null;
        }
        if(listf1 == null) {
            return listf2;
        }
        liste1.next = listf2;
        return listf1;
    }

    //方法二：实验失败，无法通过全部测试用例
    public ListNode partition(ListNode pHead, int x) {
        // write code here
        ListNode temp = null;
        ListNode cur = pHead;
        boolean flag = false;
        if(pHead != null) {
            if(pHead.val >= x) {
                while(cur != null) {
                    ListNode curNext = cur.next;
                    if(curNext != null && curNext.val < x) {
                        flag = true;
                        cur.next = curNext.next;
                        pHead = curNext;
                        temp = pHead;
                        break;
                    }
                    cur = curNext;
                }
            }
        }
        while(cur != null) {
            ListNode curNext = cur.next;
            if(flag == false && curNext != null &&
                    cur.val < x && curNext.val >= x) {
                flag = true;
                temp = cur;
                cur = cur.next;
                continue;
            }
            if(flag && curNext != null && curNext.val < x) {
                cur.next = curNext.next;
                curNext.next = temp.next;
                temp.next = curNext;
                temp = temp.next;
            } else {
                cur = cur.next;
            }
        }
        return pHead;
    }

}

public class    Test {


    public static MySingleList.ListNode mergeTwoLists(MySingleList.ListNode head1,
                                                      MySingleList.ListNode head2) {
        MySingleList.ListNode newHead = new MySingleList.ListNode(0);
        MySingleList.ListNode temp = newHead;
        while(head1 != null && head2 != null) {
            if(head1.val < head2.val) {
                temp.next = head1;
                head1 = head1.next;
            } else {
                temp.next = head2;
                head2 = head2.next;
            }
            temp = temp.next;
        }
        if(head1 != null) {
            temp.next = head1;
        }
        if(head2 != null) {
            temp.next = head2;
        }
        return newHead.next;
    }

    public static void main(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(8);
        mySingleList1.addLast(54);
        mySingleList1.addLast(32);
        mySingleList1.addLast(11);
        mySingleList1.addLast(2);
        /*mySingleList1.addLast(52);*/
        mySingleList1.display();
        /*mySingleList1.remove(22);
        mySingleList1.display();*/
        /*mySingleList1.clear();*/
        mySingleList1.partition(mySingleList1.head,22);
    }

    public static void main2(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addLast(12);
        mySingleList1.addLast(22);
        mySingleList1.addLast(32);
        mySingleList1.addLast(42);
        mySingleList1.addLast(52);
        mySingleList1.display();

        MySingleList mySingleList2 = new MySingleList();
        mySingleList2.addLast(13);
        mySingleList2.addLast(45);
        mySingleList2.addLast(67);
        mySingleList2.addLast(89);
        mySingleList2.addLast(90);
        mySingleList2.display();


        MySingleList.ListNode newHead =
                mergeTwoLists(mySingleList1.head,mySingleList2.head);
        mySingleList1.head = newHead;
        mySingleList1.display();
    }
    public static void main1(String[] args) {
        MySingleList mySingleList1 = new MySingleList();
        mySingleList1.addFirst(12);
        mySingleList1.addFirst(22);
        mySingleList1.addFirst(32);
        mySingleList1.addFirst(42);
        mySingleList1.addFirst(52);
        mySingleList1.addIndex(0,5);
        mySingleList1.addIndex(3,4);
        mySingleList1.addIndex(7,7);
        mySingleList1.display();
        System.out.println("=========================");
        boolean ret = mySingleList1.contains(4);
        System.out.println(ret);
    }
}
